package com.coder4j.controller;

import com.coder4j.service.SControlDetailService;
import com.coder4j.service.SControlService;
import com.coder4j.service.STaskFlowDetailService;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.web.R;
import com.coder4j.vo.SControlDetailQuery;
import com.coder4j.vo.SControlDetailVO;
import com.coder4j.vo.STaskFlowDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 任务流程明细模块
 * @author keller
 * @date 2025-03-18 16:22:25
 */
@Slf4j
@RestController
@RequestMapping("/sControlDetail")
public class SControlDetailController {
    @Resource
    private SControlDetailService service;

    @Resource
    private HttpServletResponse response;

    @Resource
    private SControlService sControlService;

    @Resource
    private STaskFlowDetailService sTaskFlowDetailService;

    /**
     * 按ID查询任务流程表
     * @param id SControlDetail.id
     * @return 任务流程表对象
     */
    @GetMapping("/getById")
    public R<SControlDetailVO> getById( Integer id){

        return R.success(service.baseGetVOById(id));
    }

    /**
     * 保存任务流程表
     * 有 id 字段，更新；否则新增
     * @param sControlDetail SControlDetail对象
     * @return SControlDetail对象
     */
    @PostMapping("/save")
    public R save(@RequestBody SControlDetailVO sControlDetail){

        String taskDetailIds = sControlDetail.getTaskDetailIds();
        String[] split = taskDetailIds.split(",");
        STaskFlowDetailVO sTaskFlowDetailVO = sTaskFlowDetailService.baseGetVOById(split[0]);
        sControlService.putTaskId(sTaskFlowDetailVO.getTaskFlowId(),sControlDetail.getControlId());
        service.baseSaveVO(sControlDetail);
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    public R<BasePage<SControlDetailVO>> getList(@RequestBody SControlDetailQuery query){
        if(query == null){
            query = new SControlDetailQuery();
        }
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 删除任务流程表
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id SControlDetail.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    public R deleteById( Integer id){
        service.baseDeleteById(id);
        return R.success();
    }


}
